<?php 
define('ROOTS', dirname(__FILE__).'/');#本目录
define('ROOT', dirname(ROOTS).'/');#根目录
$do=isset($_GET['do'])?$_GET['do']:'0';
if(file_exists(ROOTS.'install.lock')){
	$installed=true;
	$do='0';
}
if(isset($_GET['azssk'])){
    $installed=false;
    $do='4';
}

?>
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="format-detection" content="telephone=no">
<meta charset="UTF-8">  
<title>绪飞云支付安装向导</title> 
<link rel="shortcut icon" href="/Core/assets/img/favicon.png" /> 
<link rel="stylesheet" href="./css/layui.css">
<link href="/Core/assets/user/assets/LightYearifname/css/bootstrap.min.css" rel="stylesheet">
<link href="/Core/assets/user/assets/LightYearifname/css/materialdesignicons.min.css" rel="stylesheet">
<link href="/Core/assets/user/assets/LightYearifname/css/style.min.css" rel="stylesheet">
</head>
<style>
body{background:linear-gradient(to right,#FFCCCC,#CCCCFF);font-family:"微软雅黑";
}
@media only screen and (min-width:700px ) {
	.content{
		left: 50%;
		margin-left: -25%;
	}
}.img{
	width: 7em;
	height: 7em;
	margin:auto;
	display: block;
	border-radius: 10em;
	box-shadow: 3px 3px 8px 1px silver;
	margin-bottom: 1em;
}
.imgbj{
	width: auto;
	height: auto;
	opacity: 0.6;
	top: 0;
	left: 0;
	z-index: -1;
	position: fixed;
}
</style>
<body layadmin-themealias="dark-blue" style="margin-top: 1em;">	
<div class="layui-fluid">
	<div class="a layui-anim layui-anim-fadein">
    	<div class="layui-row layui-col-space15">
           <div class="layui-col-sm6 content">
            <div class="layui-card">
            	<div class="layui-card-header" style="height: 3em; line-height: 3em;">
            		<h3>
            		<i class="layui-icon layui-icon-search" style="font-size: 0.9em;color: #66CC99;">绪飞云支付系统安装向导</i>
            		</h3>
            	</div>
<div class="layui-card-body">
<img class="img" src="/Core/assets/img/favicon.png">
<?php if ($do==0) : ?>
<div class="layui-form">
<div class="layui-form-item">
    <div class="layui-input-company">

<center><h1></h1><font color=#66CC99>欢迎使用绪飞云支付系统！</font></h1></center>
    </div>
</div>
		<?php if($installed): ?>
<div class="alert alert-danger" role="alert">检测到您已安装过本源码，若需要重新安装，请删除本目录下的install.lock</div>
		<?php endif; ?>
		<?php if(!$installed): ?>
        <div class="layui-form-item">
            <div class="layui-input-company">
              <a href="?do=1"><button  class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid"  lay-filter="formDemo">下一步</button></a>
            </div>
        </div>
    <?php endif; ?>
                </div>
     		</div>
         </div>
      </div>	  
  </div>
</div>
<?php endif; ?>
<?php if ($do==1) : ?>
    <div class="layui-form">
    <div class="layui-form-item">
        <div class="layui-input-company">
        <form action="?do=2" method="post">
          <input type="text" id="db_host" style="text-align: center;" name="db_host"  lay-verify="required" lay-verType="tips" placeholder="数据库地址" value="localhost" class="layui-input"></br>
                <input type="text" id="db_port" style="text-align: center;" name="db_port"  lay-verify="required" lay-verType="tips" placeholder="数据库端口" value="3306" class="layui-input"></br>
                      <input type="text" id="db_name" style="text-align: center;" name="db_name"  lay-verify="required" lay-verType="tips" placeholder="数据库名" class="layui-input"></br>
                            <input type="text" id="db_user" style="text-align: center;" name="db_user"  lay-verify="required" lay-verType="tips" placeholder="数据库用户名" class="layui-input"></br>
                                  <input type="text" id="db_pwd" style="text-align: center;" name="db_pwd"  lay-verify="required" lay-verType="tips" placeholder="数据库密码" class="layui-input"><br>
                                  <input type="text" id="db_sqm" style="text-align: center;" name="db_sqm"  lay-verify="required" lay-verType="tips" placeholder="授权码" class="layui-input"><br>
                                  <p align="center">还未授权？点击<a class="layui-link" href="https://auth.daoh.top/">前往绪飞云支付授权官网</a>授权</p>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-company">
                                  <s><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid" type="submit"  lay-filter="formDemo">点我安装</button></s>
                                </div>
                            </div>
                            </form>
                            <center><font color=#66CC99>请确保数据库账号密码填写正确</font></center>
                    	</div>
                     </div>
                    </div>
                </div>
        	</div>
        </div>
<?php endif; ?>
<?php 
if($do==2){
    require 'db.class.php';
    $db_host=isset($_POST['db_host'])?$_POST['db_host']:NULL;
	$db_port=isset($_POST['db_port'])?$_POST['db_port']:NULL;
	$db_user=isset($_POST['db_user'])?$_POST['db_user']:NULL;
	$db_pwd=isset($_POST['db_pwd'])?$_POST['db_pwd']:NULL;
	$db_name=isset($_POST['db_name'])?$_POST['db_name']:NULL;
    $db_sqm=isset($_POST['db_sqm'])?$_POST['db_sqm']:NULL;
    if($db_host==null || $db_port==null || $db_user==null || $db_pwd==null || $db_name==null || $db_sqm==null){
		echo '<div class="alert alert-danger" role="alert">请填写配置信息！！</div>';
	}else{
	    $config="<?php
/*数据库配置*/
\$dbconfig=array(
	'host' => '{$db_host}', //数据库服务器
	'port' => {$db_port}, //数据库端口
	'user' => '{$db_user}', //数据库用户名
	'pwd' => '{$db_pwd}', //数据库密码
	'dbname' => '{$db_name}', //数据库名
	'xfmzfsqm' => '{$db_sqm}', //授权码
);
?>";
        if(!$con=DB::connect($db_host,$db_user,$db_pwd,$db_name,$db_port)){
            if(DB::connect_errno()==2002){
                echo '<div class="alert alert-danger" role="alert">链接失败，数据库地址错误！</div>';
            }else if(DB::connect_errno()==1045){
                echo '<div class="alert alert-danger" role="alert">链接失败，账号或密码错误！</div>';
            }else if(DB::connect_errno()==1049){
                echo '<div class="alert alert-danger" role="alert">链接失败，数据库名不存在！</div>';
            }else{
                echo '<div class="alert alert-danger" role="alert">链接数据库失败！</div>';
            }
        }else if(file_put_contents(ROOT.'/Core/Config.php',$config)){
            if(DB::query("select * from xiamu_data where 1")==FALSE){
                echo '<div class="alert alert-success" role="alert">配置文件保存成功，点击下一步</div>
                	 <div class="layui-form-item">
                    <div class="layui-input-company">
                      <a href="?do=3"><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid"  lay-filter="formDemo">导入数据表</button></a>
                    </div>
                </div>';
            }else{
                echo '<div class="alert alert-success" role="alert">检测到你安装过本程序</div>
                    			<div class="layui-form-item">
                        <div class="layui-input-company">
                          <a href="?do=4"><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid"  lay-filter="formDemo">跳过安装</button></a>
                        </div>
                    </div></hr>
    					<div class="layui-form-item">
                    <div class="layui-input-company">
                      <a href="?do=3"><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid"  lay-filter="formDemo">重新安装(清除所有数据)</button></a>
                    </div>
                </div>';
            }
        }else{
            echo '<div class="alert alert-danger" role="alert">保存失败，请检查文件权限！</div>';
        }
	}
}else if($do == 3){
    @include_once ROOT.'/Core/Config.php';
    if(!$dbconfig['user']||!$dbconfig['pwd']||!$dbconfig['dbname']){
        echo '<div class="alert alert-danger" role="alert">请先保存好配置文件！</div>';
    }else{
        require ROOTS.'db.class.php';
        $sql=file_get_contents("install.sql");
        $sql=explode(';',$sql);
        $cn = DB::connect($dbconfig['host'],$dbconfig['user'],$dbconfig['pwd'],$dbconfig['dbname'],$dbconfig['port']);
        if (!$cn){
            die('err:'.DB::connect_error());
        }else{
            DB::query("set sql_mode = ''");
            DB::query("set names utf8");
            $t=0; $e=0; $error='';
            for($i=0;$i<count($sql);$i++){
                if ($sql[$i]==''){
                    continue;
                }
                if(DB::query($sql[$i])){
                    ++$t;
                }else{
                    ++$e;
                    $error.=DB::error().'<br/>';
                }
            }
            date_default_timezone_set("PRC");
            $date = date("Y-m-d");
            DB::query("INSERT INTO `jqr_config` VALUES ('build', '".$date."')");
        }
        if($e==0) {
            echo '<div class="alert alert-success" role="alert">安装成功：成功'.$t.'句/失败'.$e.'句</div></hr></br></br>
        		<div class="layui-form-item">
                <div class="layui-input-company">
                  <a href="?do=4"><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid" lay-filter="formDemo">点我下一步</button></a>
                </div>
            </div>';
        }else{
            echo '<div class="alert alert-danger" role="alert">安装失败：成功'.$t.'句/失败'.$e.'句</br>错误信息：'.$error.'</div>
            		</hr>
            		<div class="layui-form-item">
                <div class="layui-input-company">
                  <a href="?do=3"><button class="layui-btn layui-btn layui-btn-lg layui-btn-normal layui-btn-fluid" lay-filter="formDemo">点我重试</button></a>
                </div>
            </div>';
        }
    }
}else if($do == 4){
         @file_put_contents("install.lock",'绪飞云支付安装锁');
    	echo '
    	<div class="alert alert-success" role="alert">安装成功：管理员账号密码admin/123456	</br></hr><a href="/">>>网站首页</a></hr><a href="/Admin">>>网站后台</a></div>
    <hr/><br/><font color="#FF0033">如果你的空间不支持本地文件读写，请自行在Install/ 目录建立 install.lock 文件！</font></div>';
}
?>
<script type="text/javascript" src="/Core/assets/user/assets/LightYearifname/js/jquery.min.js"></script>
<script type="text/javascript" src="/Core/assets/user/assets/LightYearifname/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/Core/assets/user/assets/LightYearifname/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="/Core/assets/user/assets/LightYearifname/js/main.min.js"></script>
</body>
</html>